﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace LotteryAdmin
{
    public partial class PlatformNews : UserControl
    {
        private LotteryAdminClient srv;
        public PlatformNews()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PlatformNews_Loaded);
            srv = WcfProxy.GetProxy;
            srv.GetNewsCompleted += new EventHandler<GetNewsCompletedEventArgs>(srv_GetNewsCompleted);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        void PlatformNews_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void srv_GetNewsCompleted(object sender, GetNewsCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            if (e.Result.Count == 0)
            {
                MessageBox.Show("没有找到匹配的记录!");
            }
            PagedCollectionView pcw = new PagedCollectionView(e.Result);
            this.dataPager.Source = pcw;
            this.dgList.ItemsSource = pcw;
        }
        void LoadGrid()
        {
            int type = Convert.ToInt32((cbxType.SelectedItem as ComboBoxItem).Tag.ToString());
            int enable = Convert.ToInt32((cbxEnable.SelectedItem as ComboBoxItem).Tag.ToString());
            srv.GetNewsAsync(type, enable);
            loadingProgress.IsBusy = true ;
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            LoadGrid();
        }
        /// <summary>
        /// ChangeEnable
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            News model = (sender as Button).Tag as News;
            if (model.IsEnable == 0)
                model.IsEnable = 1;
            else
                model.IsEnable = 0;
            srv = WcfProxy.GetProxy;
            srv.ChangeNewEnableCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result)
                {
                    (sender as Button).Tag = model;
                    if (model.IsEnable == 0)
                    {
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Green);
                        (sender as Button).Content = "启用";
                    }
                    else
                    {
                        (sender as Button).Foreground = new SolidColorBrush(Colors.Red);
                        (sender as Button).Content = "关闭";
                    }
                }
            };
            srv.ChangeNewEnableAsync(model);
            loadingProgress.IsBusy = true;
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ChangeNews cn = new ChangeNews();
            cn.ShowAddWindow();
            cn.Closed += (s, er) =>
            {
                LoadGrid();
            };
        }
        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dataPager.PageCount * dataPager.PageIndex+e.Row.GetIndex() + 1).ToString();
            (this.dgList.Columns[5].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            (this.dgList.Columns[6].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            if ((e.Row.DataContext as News).NewsType == 0)
                (this.dgList.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Orange);
            else if((e.Row.DataContext as News).NewsType == 2)
                (this.dgList.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Magenta);
            else
                (this.dgList.Columns[2].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Green);
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            LotteryAdminClient _srv = WcfProxy.GetProxy;
            _srv.DeleteNewsAsync((sender as Button).Tag as News);
            _srv.DeleteNewsCompleted += (sr, er) =>
                {
                    if (er.Result)
                    {
                        PagedCollectionView pageView = (PagedCollectionView)(dgList.ItemsSource);
                        System.Collections.ObjectModel.ObservableCollection<News> list
                            = (System.Collections.ObjectModel.ObservableCollection<News>)pageView.SourceCollection;
                        list.Remove((sender as Button).Tag as News);
                        MessageBox.Show("删除公告操作成功!");
                    }
                    else
                        MessageBox.Show("删除公告操作失败!");
                };
        }
        private void BtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            ChangeNews cn = new ChangeNews();
            cn.ShowUpdateWindow((sender as Button).Tag as News);
            cn.Closed += (s, er) =>
            {
                LoadGrid();
            };
        }
    }
}
